package com.cictec.middleware.tde.rm.disp.receiver.handler;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.feign.distribute.MiddlewareDeviceFileUploadService;
import com.cictec.middleware.tde.rm.disp.annotation.HandleMessages;
import com.cictec.middleware.tde.rm.disp.receiver.AbstractReceiverHandler;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x4BTerminalMessage;

/**
 * 升级报站文件后，设备返回结果
 * 
 * @author wsl
 *
 */
@Component
@HandleMessages(0x4B)
public class TerminalUpdateBusFileResultHandler extends AbstractReceiverHandler {

	private static Logger logger = LoggerFactory.getLogger(TerminalUpdateBusFileResultHandler.class);

	@Autowired
	private MiddlewareDeviceFileUploadService middlewareDeviceFileUploadService;

	@SuppressWarnings("rawtypes")
	@Override
	public void handleMessage(TerminalMessage message, IoSession session) {
		// 0x80 指令应答 收到需要应答的数据包
		sendMessageToTerminal(createResponse(message), session);
		
		Rm0x4BTerminalMessage msg = (Rm0x4BTerminalMessage) message;
		// System.out.println("【0x4B设备指令应答】"+JSONObject.toJSONString(tubfr));
		logger.debug("【0X4B结束传输】终端 {} Session {} ", msg.getTerminalId(), msg.getSessId());

		String nowSessinoId = msg.getSessId();
		String devCode = PMSUtils.isNull(msg.getHeader().getTerminalId());
		Result resultMap = middlewareDeviceFileUploadService.uploadDeviceStaionFileUploadSessionId(PMSUtils.isNull(msg.getHeader().getTerminalId()), nowSessinoId);
		if (ResultUtil.isSuccess(resultMap)) {
			Map data = new HashMap();
			nowSessinoId = PMSUtils.isNull(data.get("sessionId"));
		}

		String info = "";
		int result = msg.getResult();
		if (result == 2 || result == 0) {// 升级成功
			if (result == 0) {
				info = "下载成功";
			} else {
				info = "升级成功";
			}
			// 更新详情，成功
			middlewareDeviceFileUploadService.uploadDeviceStaionFileUploadResult(devCode, nowSessinoId, "2", "");

		} else if (result == 1 || result == 3) {// 失败

			int reason = msg.getFailureReason();
			switch (reason) {
			case 1:
				// 连接服务器失败
				info = "连接服务器失败";
				break;
			case 2:
				// 验证失败
				info = "验证失败";
				break;
			case 3:
				// 下载文件失败
				info = "下载文件失败";
				break;
			case 4:
				// 文件校验错误
				info = "文件校验错误";
				break;
			case 5:
				// 无需升级
				info = "无需升级";
				break;
			case 6:
				// 其他原因
				info = "其他原因";
				break;
			}

			// 更新详情，失败
			middlewareDeviceFileUploadService.uploadDeviceStaionFileUploadResult(devCode, nowSessinoId, "3", info);

		}
	}

}
